package it.polimi.reTravelDream.beans;

import it.polimi.reTravelDream.dto.FlightDTO;
import it.polimi.reTravelDream.dto.HotelDTO;
import it.polimi.reTravelDream.dto.PrenotationDTO;
import it.polimi.reTravelDream.dto.TravelPackageDTO;
import it.polimi.reTravelDream.eao.HotelEAOLocal;
import it.polimi.reTravelDream.eao.PrenotationEAOLocal;
import it.polimi.reTravelDream.eao.TravelPackageActionEAOLocal;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="hotels")
@ViewScoped

public class HotelAction {

		@EJB
		private HotelEAOLocal hotelAction;
		
		@EJB
		private TravelPackageActionEAOLocal travelPackageActionEAOLocal;
		
		@EJB
		private PrenotationEAOLocal prenotationEAOLocal;
		
		
		private List<HotelDTO> hotelList;
		private boolean isEdit;
		
		private HotelDTO selectedHotel;
		
		private String errorDelete;
		private String errorEdit;
		
		private String errorPrice;
		private String errorName;
		private String errorDescription;
		private String errorCity;
		private String errorAddress;
		
		private boolean isProva=false;

		
		public HotelAction(){
			this.isEdit=false;
			this.hotelList=new ArrayList<HotelDTO>();
			this.selectedHotel=new HotelDTO();
		}
		
		@PostConstruct
		public void getAllHotel(){
			this.hotelList=this.hotelAction.getAllHotel();
		}

		
		public void removeHotel(HotelDTO hotelToRemove){
			System.out.println("remove hotel");
			this.isProva=false;
			this.errorDelete="";
			this.selectedHotel=hotelToRemove;
			boolean value=true;
			
			if (!this.checkIfPresentInAPackage()) {
				System.out.println("in a package");
				this.errorDelete="the Hotel "+hotelToRemove.getId()+" is part of a package,"
						+ "if you want to delete it from the system you must first remove it from"
						+ " the package and then you can delete it from the system. Otherwise you can disable the item "
						+ "preventing it"
						+ "to be used again (you can re enabled it later if you want)";
				value=false;				
			}
			if (!this.checkIfPresentInAPrenotation()) {
				System.out.println("in a prenotation");

				this.errorDelete="the Hotel "+hotelToRemove.getId()+" is part of a prenotation,"
						+ "you cannot delete it from the system. You can disable the item "
						+ "preventing it"
						+ "to be used again (you can re enabled it later if you want)";
				value=false;
			}
			
			if (!value) {
				this.isProva=true;
				System.out.println("return");
				return;
			}
			
			try {
				this.hotelAction.removeHotel(hotelToRemove);
				this.hotelList=this.hotelAction.getAllHotel();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

	
		public void editButton(HotelDTO hotelToEdit){
			this.isProva=false;
			this.isEdit=true;
			this.errorDelete="";
			this.selectedHotel=hotelToEdit;
			
			
		}
		
		public void saveEdit(){
				boolean value=true;
				this.isProva=false;
				
				
		
			if (!this.checkIfPresentInAPackage()) {
				System.out.println("in a package");
				this.errorEdit="the Hotel "+this.selectedHotel.getId()+" is part of a package,"
						+ "if you want to modify it  you must first remove it from"
						+ " the package and then you can modify it. Otherwise you can disable the item "
						+ "preventing it"
						+ "to be used again (you can re enabled it later if you want)";
				value=false;
				this.isProva=true;
			}
			
			if(!this.checkIfPresentInAPrenotation()){
				System.out.println("in a prenotation");

				this.errorEdit="the Hotel "+this.selectedHotel.getId()+" is part of a prenotation,"
						+ "you cannot modify it";
				this.isProva=true;
			}
			
			
			
			if (!value) {
				System.out.println(value);
				return;
			}
			
			if (!this.check()) {
				return;
			}	
			
			this.hotelAction.editHotel(this.selectedHotel);
			this.isEdit=false;
			this.isProva=false;
			this.hotelList=this.hotelAction.getAllHotel();
		}
		
		public void cancel(){
			this.isEdit=false;
			this.isProva=false;

		}
		
		
		
		private boolean check(){
			this.errorAddress="";
			this.errorDescription="";
			this.errorName="";
			this.errorPrice="";
			this.errorCity="";
						
			boolean value=true;
			

			if (!this.checkPrice()) {
				this.errorPrice="price not valid";
				value=false;
			}
			if(!this.checkName()){
				this.errorName="the name can not be empty";
				value=false;
			}
			if (!this.checkDescription()) {
				this.setErrorDescription("the description can not be empty ");
				value=false;
			}
			if (!this.checkCity()) {
				this.errorCity="you must choose a city";
				value=false;
			}
			if (!this.checkAddress()) {
				this.errorAddress="address not valid";
				value=false;
			}
			return value;
		}
		
		
		private boolean checkPrice(){
				if(this.selectedHotel.getPrice()==0){
					System.out.println("price is 0!!!");
					return false;
				}
			return true;
		}
	
		private boolean checkDescription(){
			if(this.selectedHotel.getDescription().isEmpty()){
				return false;
			}
			return true;
		}
		
		private boolean checkName(){
			
			if (this.selectedHotel.getName().isEmpty()) {
				return false;
			}
		return true;
		}
		
		private boolean checkCity(){
			if (this.selectedHotel.getCity().isEmpty()) {
				return false;
			}
			return true;
		}
		
		private boolean checkAddress(){
			if(this.selectedHotel.getAddress().isEmpty()){
				return false;
			}
			return true;
		}

		
		public void activate(HotelDTO hotelToActivate){
			hotelToActivate.setActive(true);
			try {
				this.hotelAction.editHotel(hotelToActivate);
				this.hotelList=this.hotelAction.getAllHotel();
			} catch (Exception e) {
				
			}
			
		}
		
		
		
		
		public void disable(HotelDTO hotelToDisable){
			hotelToDisable.setActive(false);
			try {
				this.hotelAction.editHotel(hotelToDisable);
				this.hotelList=this.hotelAction.getAllHotel();
			} catch (Exception e) {
				
			}
		}
		
		
		
		private boolean checkIfPresentInAPackage(){
			List<TravelPackageDTO> allTravelPackageDTOs=this.travelPackageActionEAOLocal.getAllPackages();	
			
			for(TravelPackageDTO tDto:allTravelPackageDTOs){
				List<HotelDTO> hotelInThePAckage=tDto.getPackageHotelDTOs();
							
				for(HotelDTO hotelDTO:hotelInThePAckage){				
						if (this.selectedHotel.getId()==hotelDTO.getId()) {
							return false;					
						}
					}
			}
			return true;
		}
		
		private boolean checkIfPresentInAPrenotation(){
			List<PrenotationDTO> prenotationDTOs=this.prenotationEAOLocal.getAllPrenotation();
			for(PrenotationDTO prenotationDTO:prenotationDTOs){
				if (this.selectedHotel.getId()==prenotationDTO.getHotelChosen().getId()) {
					return false;
				}
			}
			return true;
		}
		
	
		
		
		/**
		 * 
		 * getter and setter
		 */
		
		public List<HotelDTO> getHotelList() {
			return hotelList;
		}

		public void setHotelList(List<HotelDTO> hotelList) {
			this.hotelList = hotelList;
		}
		
	
		public boolean isEdit() {
			return isEdit;
		}

		public void setEdit(boolean isEdit) {
			this.isEdit = isEdit;
		}

		public HotelDTO getSelectedHotel() {
			return selectedHotel;
		}

		public void setSelectedHotel(HotelDTO selectedHotel) {
			this.selectedHotel = selectedHotel;
		}

		public String getErrorDelete() {
			return errorDelete;
		}

		public void setErrorDelete(String errorDelete) {
			this.errorDelete = errorDelete;
		}

		public String getErrorEdit() {
			return errorEdit;
		}

		public void setErrorEdit(String errorEdit) {
			this.errorEdit = errorEdit;
		}

		public boolean isProva() {
			return isProva;
		}

		public void setProva(boolean isProva) {
			this.isProva = isProva;
		}

		public String getErrorPrice() {
			return errorPrice;
		}

		public void setErrorPrice(String errorPrice) {
			this.errorPrice = errorPrice;
		}

		public String getErrorName() {
			return errorName;
		}

		public void setErrorName(String errorName) {
			this.errorName = errorName;
		}

		public String getErrorDescription() {
			return errorDescription;
		}

		public void setErrorDescription(String errorDescription) {
			this.errorDescription = errorDescription;
		}

		public String getErrorCity() {
			return errorCity;
		}

		public void setErrorCity(String errorCity) {
			this.errorCity = errorCity;
		}

		public String getErrorAddress() {
			return errorAddress;
		}

		public void setErrorAddress(String errorAddress) {
			this.errorAddress = errorAddress;
		}



}

